<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="产品名称" prop="serviceName">
        <el-input
          v-model="queryParams.serviceName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.state" clearable placeholder="请选择">
          <el-option
            v-for="dict in status"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-select v-model="queryParams.sort" clearable placeholder="请选择">
          <el-option
            v-for="dict in sortType"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['information:center:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-sort"
          size="mini"
          @click="toggleExpandAll"
        >展开/折叠
        </el-button>
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['information:center:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="centerList"
      row-key="id"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      height="650" fixed
    >
      <el-table-column label="ID" prop="id"/>
      <el-table-column label="企业名称" prop="serviceName">
        <template slot-scope="scope">
          <span>亦朋科技产品体系</span>
        </template>
      </el-table-column>
      <el-table-column label="产品名称" prop="serviceName"/>
      <el-table-column label="所在位置" align="center" prop="levelNumber">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.system_level" :value="scope.row.levelNumber"/>
        </template>
      </el-table-column>
      <el-table-column  width="220" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-right"
            @click="handleRecom(scope.row)"
            v-hasPermi="['information:center:edit']"
            v-if="scope.row.state===0"
          >禁用
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-right"
            @click="handleRecom(scope.row)"
            v-hasPermi="['information:center:edit']"
            v-if="scope.row.state===1"
          >启用
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['information:center:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['information:center:add']"
          >新增
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['information:center:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

<!--    <pagination-->
<!--      v-show="total>0"-->
<!--      :total="total"-->
<!--      :page.sync="queryParams.pageNum"-->
<!--      :limit.sync="queryParams.pageSize"-->
<!--      @pagination="getList"-->
<!--    />-->

    <!-- 添加或修改体系分类对话框 -->
    <el-dialog :title="title" :visible.sync="open" :close-on-click-modal='false' width="759px" append-to-body center>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="所在位置" prop="levelNumber">
          <el-select v-model="form.levelNumber" disabled placeholder="请选择需求标题">
            <el-option
              v-for="dict in dict.type.system_level"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="上级名称" prop="parentId">
          <treeselect v-model="form.parentId" :options="centerOptions" :normalizer="normalizer"
                      placeholder="请选择上级"/>
        </el-form-item>
        <el-form-item label="产品名称" prop="serviceName">
          <el-input v-model="form.serviceName" placeholder="请输入产品名称"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addCenter, delCenter, getCenter, listCenter, updateCenter} from "@/api/information/center";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Center",
  dicts: ['system_level'],
  components: {
    Treeselect
  },
  data() {
    return {
      // 总条数
      total: 0,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 体系分类表格数据
      centerList: [],
      // 体系分类树选项
      centerOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      //所有数据
      centerAll: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        serviceName: null,
        levelNumber: null,
        parentId: null,
        sort: null,
        state: null
      },
      sortType: [
        {
          value: 0,
          label: "按时间倒序"
        },
        {
          value: 1,
          label: "按时间正序"
        },
      ],
      status: [
        {
          value: 0,
          label: "启用"
        },
        {
          value: 1,
          label: "禁用"
        },
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        serviceName: [
          {required: true, message: "产品名称不能为空", trigger: "blur"}
        ],
        levelNumber: [
          {required: true, message: "所在位置不能为空", trigger: "change"}
        ],
        parentId: [
          {required: true, message: "上级服务不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  watch: {
    "form.parentId": {
      immediate:true,
      handler(newValue, oldValue) {
        let n = 0
        this.centerAll.forEach(item => {
          if (item.id === newValue) {
            this.form.levelNumber = Number(item.levelNumber) + 1
            n++
          }
        })
        if (n === 0) {
          this.form.levelNumber = 1
        }
      }
    }

  },
  methods: {
    /** 启禁用按钮操作 */
    handleRecom(row) {
      const id = row.id
      if (row.state === 0) {
        this.$modal.confirm('是否确认禁用编号为"' + id + '"的数据项？').then(function () {
          row.state = 1
          return updateCenter(row)
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("禁用成功");
        }).catch(() => {
        });
      } else {
        this.$modal.confirm('是否确认启用编号为"' + id + '"的数据项？').then(function () {
          row.state = 0
          return updateCenter(row)
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("启用成功");
        }).catch(() => {
        });
      }

    },
    /** 查询体系分类列表 */
    getList() {
      this.loading = true;
      listCenter(this.queryParams).then(response => {
        this.centerList = this.handleTree(response.data, "id", "parentId");
        this.loading = false;
        this.centerAll = response.data
        this.total=response.data.length
      });
    },
    /** 转换体系分类数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.serviceName,
        children: node.children
      };
    },
    /** 查询体系分类下拉树结构 */
    getTreeselect() {
      listCenter().then(response => {
        this.centerOptions = [];
        const data = {id: 0, serviceName: '顶级节点', children: []};
        data.children = this.handleTree(response.data, "id", "parentId");
        this.centerOptions.push(data);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        serviceName: null,
        title: null,
        releaseName: null,
        releaseTime: null,
        linkType: null,
        linkVal: null,
        levelNumber: null,
        parentId: null,
        note: null,
        sort: null,
        state: null,
        dr: null,
        createTime: null,
        updateTime: null
      };
      this.form.parentId = null
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getTreeselect();
      if (row != null && row.id) {
        this.form.parentId = row.id;
      } else {
        this.form.parentId = 0;
      }
      this.open = true;
      this.title = "添加体系分类";
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      if (row != null) {
        this.form.parentId = row.id;
      }
      getCenter(row.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改体系分类";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCenter(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCenter(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              this.form.parentId = null
            });
          }
        } else {
          this.form.parentId = this.form.parentId
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除体系分类编号为"' + row.id + '"的数据项？').then(function () {
        return delCenter(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('informfrontend/centerBack/export', {
        ...this.queryParams
      }, `体系分类_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
